package com.ecnu.system.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ecnu.common.utils.bean.BeanUtils;
import com.ecnu.system.domain.ProjectQA;
import com.ecnu.system.domain.dto.ProjectQADTO;
import com.ecnu.system.mapper.ProjectQAMapper;
import com.ecnu.system.service.IProjectQAService;
import org.springframework.stereotype.Service;

import javax.validation.constraints.Size;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Collections;
import java.util.List;

@Service
public class ProjectQAServiceImpl extends ServiceImpl<ProjectQAMapper, ProjectQA> implements IProjectQAService {


    @Override
    public boolean save(ProjectQADTO projectQADTO, @Size(min = 0, max = 30, message = "用户昵称长度不能超过30个字符") String nickName) {
        ProjectQA projectQA = new ProjectQA();
        BeanUtils.copyProperties(projectQADTO, projectQA);
        projectQA.setCreate_user(nickName);
        //插入当前时间
//        projectQA.setCreate_time(LocalDateTime.from(LocalDate.now()));
        return this.save(projectQA);
    }

    @Override
    public boolean update(ProjectQADTO projectQADTO, String nickName) {
        ProjectQA projectQA = new ProjectQA();
        BeanUtils.copyProperties(projectQADTO, projectQA);
        projectQA.setUpdate_user(nickName);
        //插入当前时间
//        projectQA.setUpdate_time(LocalDateTime.from(LocalDate.now()));
        return this.updateById(projectQA);
    }

    @Override
    public boolean remove(int[] ids, String nickName) {
        List<ProjectQA> projectQAS = this.listByIds(Collections.singleton(ids));
        if (projectQAS.isEmpty()) {
            return false;
        }
        for (ProjectQA projectQA : projectQAS) {
            projectQA.setUpdate_user(nickName);
            //插入当前时间
            projectQA.setUpdate_time(LocalDateTime.from(LocalDate.now()));
            //删除标识
            projectQA.setDel_flag("2");
        }
        return this.updateBatchById(projectQAS);
    }

    @Override
    public boolean addText(ProjectQADTO projectQADTO, String nickName) {
        ProjectQA projectQA = new ProjectQA();
        BeanUtils.copyProperties(projectQADTO, projectQA);
        projectQA.setCreate_user(nickName);
        if(projectQA.getRichText()==null){
            projectQA.setRichText("");
        }

        return this.save(projectQA);
    }

    @Override
    public boolean updateText(ProjectQADTO projectQADTO, String nickName) {
        ProjectQA projectQA = new ProjectQA();
        BeanUtils.copyProperties(projectQADTO, projectQA);
        projectQA.setUpdate_user(nickName);
        if(projectQA.getRichText()==null){
            projectQA.setRichText("");
        }

        return this.updateById(projectQA);
    }
}
